<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <form class="layui-form" lay-filter="myform" id="myform">
                    <input type="reset" id="btReset" class="layui-hide" >
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">入学年级</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" id="name"  autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-btn-group">
                                <button type="button" class="layui-btn layui-btn-primary" data-type="query"><i class="layui-icon">&#xe615;</i>查询</button>
                                <button type="button" class="layui-btn layui-btn-primary" data-type="resetBtn"><i class="layui-icon">&#xe669;</i>重置</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="row">
                <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
            </div>
        </div>
    </div>
</div>

<script th:inline="none"></script>
<script>
    layui.use(['table','form','element'], function(){
        var table = layui.table,
            element = layui.element,
            form = layui.form;
        table.render({
            elem: '#main-table'
            ,height: (top.height-85)+'px'
            ,url: 'grade/getPageList'
            ,title: '题库列表'
            ,page: true
            ,toolbar: '#toolbarPrice'
            ,cols: [
                [
                    {type: 'checkbox'}
                    ,{field: 'name', title: "入学年级",width:320,templet:function(d){
                        return "<a class='colum-link' href='javascript:void(0)' onclick='edit(\""+d.id+"\",\"" + d.name + "\" , this)'>" + d.name + "</a>";
                    }}
                ]
            ]
            ,done:function(){
                enableRowClick(table,form,'main-table',"main-table");
            }
        });
        table.on('edit(main-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            axios.post('gradeYear/updateOrderno',obj.data).then(
                res=>{
                    layer.msg('保存成功',{icon:1});
                    table.reload('main-table');
                }
            )
        });
        //头工具栏事件
        table.on('toolbar(main-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        id:"openwin",
                        type: 2,
                        title:'增加学年',
                        area:['1100px','600px'],
                        content:  'rooter?url=base/grade/add-grade',
                        btn:['保存','关闭'],
                        yes:function(index,layero){
                            var dataForm = layer.getChildFrame('form', index);
                            dataForm.find("#submitBtn").click();
                            return false
                        }
                    });
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if(data.length>0){
                        var values = {};
                        var ids=[];
                        $.each(data,function(i,v){
                            ids.push(v.id);
                        })
                        values['ids'] = ids;
                        layer.confirm('确定要删除当前行么?', {icon: 3, title:'提示'}, function(index){
                            axios.post("grade/delete",{ids:ids.join(",")}).then(res=>{
                                res = res.data;
                                if (res.code == 1) {
                                    layer.msg(res.msg);
                                } else {
                                    layer.close(index);
                                    table.reload('main-table');
                                    layer.msg("删除成功。");
                                }
                            })
                        });
                    }else{
                        layer.msg("请选择操作数据!")
                    }
                    break;
                case 'edit':
                    var data2 = checkStatus.data;
                    if(data2.length>0){
                        edit(data2[0].id);
                    }else{
                        layer.msg("请选择操作数据！")
                    }
                    break;

            };
        });
        var $ = layui.$, active = {
            query: function(){

                table.reload("main-table",{where:{name:$("#name").val()}})
            },
            resetBtn:function () {
                $("#btReset").click();

            }
        };
        window.loadGrid = function(tableName){
            table.reload(tableName);
        }
        $('.layui-form .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
    function edit(id){
        layer.open({
            id:"openwin",
            type: 2,
            title:'修改学年信息',
            area:['1100px','600px'],
            content:  'rooter?url=base/grade/add-grade&id='+id,
            btn:['保存','关闭'],
            yes:function(index,layero){
                var dataForm = layer.getChildFrame('form', index);
                dataForm.find("#submitBtn").click();
                return false
            }
        });
    }

</script>
<script type="text/html" id="toolbarPrice">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-addition"></i>增加</button>
        <button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>

    </div>
</script>
</body>
</html>